package com.guofen.service.sjck.impl;

import com.guofen.dao.SjckHistoryListMapper;
import com.guofen.dao.SjckTableListMapper;
import com.guofen.pojo.SjckGetToken;
import com.guofen.pojo.SjckHistoryList;
import com.guofen.pojo.SjckTableColumn;
import com.guofen.pojo.SjckTableList;
import com.guofen.service.sjck.HistoryListService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class HistoryListImpl implements HistoryListService {

    @Autowired
    private SjckTableListMapper tableListMapper;

    @Autowired
    private SjckHistoryListMapper historyListMapper;

    SjckGetToken getToken = new SjckGetToken();

    @Override
    public Result getAllHistoryList(String signalName,String startTime,String endTime) {

        // 找到需要查询的表有哪些
        List<SjckTableList> tableNames = tableListMapper.getTableName(startTime,endTime);
        ArrayList<SjckHistoryList> item = new ArrayList<>();
        ArrayList<SjckTableColumn> tableColumns = new ArrayList<>();
        ArrayList<HashMap<String, Object>> tableData = new ArrayList<>();
        String tag = "value";
        SjckTableColumn column = new SjckTableColumn();
//        column.setLabel("时间");column.setProp("Time");
        tableColumns.add(column);
        for (int i = 0;i < tableNames.size();i++) {
            item = historyListMapper.selectAll(tableNames.get(i).getTableName(),signalName);
            for (int j = 0;j < item.size(); j++) {
                String prop = tag + (tableColumns.size()-1);
                boolean flag = false;
                for (int k = 0;k < tableColumns.size(); k++) {
                    if (tableColumns.get(k).getLabel().equals(item.get(j).getSignalName())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    tableColumns.add(new SjckTableColumn(item.get(j).getSignalName(),prop));
                }
            }
            // 以上得到了tableColumns
            // 下面得到tableData
            for (int j = 0;j < item.size(); j++) {
                boolean flag1 = false;
                String prop = "";
                for (int k = 0;k < tableColumns.size();k++) {
                    if (tableColumns.get(k).getLabel().equals(item.get(j).getSignalName())) {
                        prop = tableColumns.get(k).getProp();
                    }
                }

                for (int k = 0;k < tableData.size();k ++) {
                    if (tableData.get(k).get("Time").equals(item.get(j).getAcquisitionTime())) {
                        flag1 = true;
//                        SjckTableDataList sjckTableDataList = new SjckTableDataList(prop,item.get(j).getValue());
//                        tableData.get(k).getDataLists().add(sjckTableDataList);
                        tableData.get(k).put(prop, item.get(j).getValue());
                        break;
                    }
                }
//                if (!flag1) {
//                    ArrayList<SjckTableDataList> sjckTableDataLists = new ArrayList<>();
//                    SjckTableDataList sjckTableDataList = new SjckTableDataList(prop,item.get(j).getValue());
//                    sjckTableDataLists.add(sjckTableDataList);
//                    SjckTableData sjckTableData = new SjckTableData(item.get(j).getAcquisitionTime(),new ArrayList<SjckTableDataList>(sjckTableDataLists));
//                    tableData.add(sjckTableData);
//                }
                if (!flag1) {
                    HashMap<String, Object> lineDataMap = new HashMap<>();
                    lineDataMap.put("Time", item.get(j).getAcquisitionTime());
                    lineDataMap.put(prop, item.get(j).getValue());
                    tableData.add(lineDataMap);
                }
            }
        }
        Map<String,ArrayList<Object>> map = new HashMap<>();
        map.put("tableData",new ArrayList<>(tableData));
        map.put("tableColumns",new ArrayList<>(tableColumns));
        return ResultGenerator.genSuccessResult(map);

        /*ArrayList<SjckHistoryList> item = new ArrayList<>();
        Map<String,Map<String,Double>> list = new HashMap<>();

        for (int i = 0;i < tableNames.size(); i++) {
            item = historyListMapper.selectAll(tableNames.get(i).getTableName(),signalName);
            // 修改item的格式为适当的格式
            Map<String,Map<String,Double>> map = new HashMap<>();
            for (int j = 0;j < item.size();j++) {
                if (map.containsKey(item.get(j).getAcquisitionTime())) {
                    map.get(item.get(j).getAcquisitionTime()).put(item.get(j).getSignalName(),item.get(j).getValue());
                }else {
                    Map<String,Double> subMap = new TreeMap<>();
                    subMap.put(item.get(j).getSignalName(),item.get(j).getValue());
                    map.put(item.get(j).getAcquisitionTime(),subMap);
                }
            }
            list.putAll(map);
        }
        return ResultGenerator.genSuccessResult(list);*/

    }

    // get温度
    @Override
    public Result getTempByEquipAndTime(String signalName,String startTime,String endTime) {

        // 根据开始和结束的时间找出来需要查询的表有哪些。
        List<SjckTableList> tableNames = tableListMapper.getTableName(startTime,endTime);
        System.out.println(tableNames.size());
        ArrayList<SjckHistoryList> list = new ArrayList<>();
        ArrayList<SjckHistoryList> items = new ArrayList<>();
        for (int i = 0;i < tableNames.size(); i++) {
            list = historyListMapper.getTempByEquipAndTime(tableNames.get(i).getTableName(),signalName);
            System.out.println(list.size());
            items.addAll(list);
        }
        return ResultGenerator.genSuccessResult(items);
    }

    @Override
    public Result getMoiByEquipAndTime(String signalName, String startTime, String endTime) {
        // 根据开始和结束的时间找出来需要查询的表有哪些。
        List<SjckTableList> tableNames = tableListMapper.getTableName(startTime,endTime);
        System.out.println(tableNames.size());
        ArrayList<SjckHistoryList> list = new ArrayList<>();
        ArrayList<SjckHistoryList> itemsMoi = new ArrayList<>();
        for (int i = 0;i < tableNames.size(); i++) {
            list = historyListMapper.getMoiByEquipAndTime(tableNames.get(i).getTableName(),signalName);
            System.out.println(list.size());
            itemsMoi.addAll(list);
        }
        return ResultGenerator.genSuccessResult(itemsMoi);
    }

    // 得到所有分设备的名称。
    @Override
    public Result getAllSignalName() {
        // 一个表名就可以。
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        String day = format.format(date);
        String tableName = "history" + day;

        String[] signalArray = historyListMapper.getNameBySignalId(tableName);

        ArrayList<String> signalNames = new ArrayList<>();

        if (signalArray.length > 0) {
            // 处理得到的signalName，除去后缀
            for (int i = 0;i < signalArray.length; i++) {
                if (signalArray[i].endsWith("_环境_温度") || signalArray[i].endsWith("_物料_温度")) {
                    signalNames.add(signalArray[i].substring(0,signalArray[i].length()-6));
                }else if (!signalArray[i].endsWith("_环境_湿度")){
                    signalNames.add(signalArray[i]);
                }
            }
            return ResultGenerator.genSuccessResult(signalNames);
        }else {
            return ResultGenerator.genFailResult("获得数据失败！");
        }
    }
}

